package cn.net.yugu.doraemon.api.common.response;

import cn.net.yugu.doraemon.api.common.enums.CommonResultCode;
import cn.net.yugu.doraemon.api.common.enums.IErrorCode;
import cn.net.yugu.doraemon.api.common.vo.ListWithPaginatorVO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * @description:
 * @author: wanghongli
 * @create: 2022-09-20 13:14:06
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PaginatorResult<T> extends BaseResult {
    private ListWithPaginatorVO<T> data;

    public PaginatorResult(Integer code, ListWithPaginatorVO<T> data, String message) {
        this.data = data;
        this.setCode(code);
        this.setMessage(message);
    }

    public static <T> PaginatorResult<T> successWithData(List<T> items, int page, int pageSize, int totalCount) {
        Paginator paginator = Paginator.builder().page(page).pageSize(pageSize).totalCount(totalCount).build();
        ListWithPaginatorVO<T> paginatorVO = new ListWithPaginatorVO<>();
        paginatorVO.setItems(items);
        paginatorVO.setPaginator(paginator);
        return new PaginatorResult<T>(CommonResultCode.SUCCESS.getCode(), paginatorVO, null);
    }

    public static <T> PaginatorResult<T> successWithData(ListWithPaginatorVO<T> data) {
        return new PaginatorResult<T>(CommonResultCode.SUCCESS.getCode(), data, null);
    }

    public static <T> PaginatorResult<T> failWithMessage(String message) {
        return new PaginatorResult<T>(CommonResultCode.FAIL.code, null, message);
    }

    public static <T> PaginatorResult<T> failWithMessage(Integer errorCode, String message) {
        return new PaginatorResult<T>(errorCode, null, message);
    }

    public static <T>PaginatorResult<T> failWithError(IErrorCode errorCode) {
        return new PaginatorResult<>(errorCode.getCode(), null, errorCode.getMessage());
    }
}
